package com.masterclient.app.businessmodule.minemodule.footprint.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.presenter.FCBaseCallBack;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.businessmodule.minemodule.api.MineHttpService;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.personal.FootprintBean;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2018/05/21
*/
public class FootprintPresenter extends FootprintContract.Presenter{

    public boolean isHasData = false;

    @Override
    public void getProductHistory(String page, String page_size,boolean isRefresh) {

        if (!isHasData && !isRefresh) {
            mView.baseShowPageLoading(false);
        }

        Observable<FootprintBean> observable = RetrofitUtil.getInstance().getProductHistory(NetEnvManager.BASE_SURL+"1/customer/get_product_history/"+page+"-"+page_size+".html");

        Disposable disposable = toSubscribe(observable, new Consumer<FootprintBean>() {
            @Override
            public void accept(@NonNull FootprintBean bean) throws Exception {
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                if("0".equals(bean.getCode())){
                    if (bean.getData().getSh_items() != null && bean.getData().getSh_items().size() > 0) {
                        isHasData = true;
                    }
                    mView.showHistoryData(bean.getData());
                }else {
                    ToastUtil.showToast(SERVER_FAILED_STR);
                    mView.baseShowError();
                    if (!isHasData) {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                    }
                }
            }
        },  new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.baseHiddenPageLoading();
                ToastUtil.showToast(WEB_FAILED_STR);
                mView.baseShowError();
                if (!isHasData) {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                }
            }
        });

        mRxManager.add(disposable);

    }

    @Override
    public void clearFootPrintInfo() {
        Observable<CommonBaseResponse> observable = MineHttpService.getInstance().clearFootPrintInfo();

        Disposable disposable = toNewSubscribe(observable, new FCBaseCallBack<CommonBaseResponse<Object>>() {
            @Override
            public void onSuccess(CommonBaseResponse<Object> bean) {
                if(0 == bean.getCode()){
                    mView.clearSuccess();
                    isHasData = false;
                }else {
                    mView.clearFailed(bean.getMsg());
                }
            }
        });
        mRxManager.add(disposable);
    }

}